﻿//date format validation
jQuery.validator.unobtrusive.adapters.add('formatdatevalidate', [], function (options) {
    options.rules['formatdatevalidate'] = options.params;
    options.messages['formatdatevalidate'] = options.message;
});
jQuery.validator.addMethod('formatdatevalidate', function (value, element, params) {
    var date = Date.parse(value);
    if (isNaN(date)) return false;
    else return true;
});
//date limit validation
jQuery.validator.unobtrusive.adapters.add('rangedatevalidation', ['mindate', 'maxdate'], function (options) {
    options.rules['rangedatevalidation'] = options.params;
    options.messages['rangedatevalidation'] = options.message;
});
jQuery.validator.addMethod('rangedatevalidation', function (value, element, params) {
    var date = Date.parse(value);
    if (isNaN(date)) return false;
    else {
        if (typeof (date) != 'number') return false;
        else {
            var mindate = Date.parse(params.mindate);
            var maxdate = Date.parse(params.maxdate);
            if (isNaN(mindate) || isNaN(maxdate)) return true;
            return mindate < date && maxdate > date;
        }
    }
});

//validation filter input
function validateInput(minTbxId,maxTbxId,minMesId,maxMesId,btnId) {

   function minfactorValidator() {
        var value = $('#' + minTbxId).val();
        var node = $('#' + minMesId);
        var number = value-0;
        if (isNaN(number)) {
            node.removeClass("field-validation-valid");
            node.addClass("field-validation-error");
            return false;
        } else {
            if (number < 0 || number > 1) {
                node.removeClass("field-validation-valid");
                node.addClass("field-validation-error");
                return false;
            }
        }
        if (node.hasClass("field-validation-error")) {
            node.removeClass("field-validation-error");
            node.addClass("field-validation-valid");
        }
       return true;
    }

    function maxfactorValidator() {
        var value = $('#' + maxTbxId).val();
        var node = $('#' + maxMesId);
        var number = value - 0;
        if (isNaN(number)) {
            node.removeClass("field-validation-valid");
            node.addClass("field-validation-error");
            return false;
        } else {
            if (number < 0 || number > 1) {
                node.removeClass("field-validation-valid");
                node.addClass("field-validation-error");
                return false;
            }
        }
        if (node.hasClass("field-validation-error")) {
            node.removeClass("field-validation-error");
            node.addClass("field-validation-valid");
        }
        return true;
    }

    function allValidator() {
        return  minfactorValidator() && maxfactorValidator();
    }

    function init(minValidation, maxValidation, allValidation) {
        $('#' + minTbxId).bind('change', minValidation);
        $('#' + maxTbxId).bind('change', maxValidation);
        $('#' + btnId).bind('click', allValidation);
    }

    init(minfactorValidator, maxfactorValidator, allValidator);
}

$(document).ready(function() {
    validateInput('minFactor', 'maxFactor', 'minFactorValidator', 'maxFactorValidator', 'submitButton');
});